package com.ruoyi.picture.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.picture.enums.EQiNiuArea;
import com.ruoyi.system.domain.SystemConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 七牛云工具类
 * @author lixin
 * @date 2021-05-31 23:04
 */
@Slf4j
@Component
public class QiniuUtil {

	/**
	 * 七牛云上传图片
	 * @param multipartFile
	 * @param qiNiuConfig
	 * @return
	 * @throws QiniuException
	 */
	public String uploadQiniu(MultipartFile multipartFile, SystemConfig qiNiuConfig, String directory) throws QiniuException {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = setQiNiuArea(qiNiuConfig.getQiNiuArea());
		//生成上传凭证，然后准备上传
		String accessKey = qiNiuConfig.getQiNiuAccessKey();
		String secretKey = qiNiuConfig.getQiNiuSecretKey();
		String bucket = qiNiuConfig.getQiNiuBucket();
		//...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		//生成的是不带-的字符串，类似于：b17f24ff026d40949c85a24f4f375d42
		String key = IdUtil.simpleUUID();
		if (StrUtil.isNotEmpty(directory)) {
			key = directory + key;
		}
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		Response response = null;
		try {
			response = uploadManager.put(multipartFile.getBytes(), key, upToken);
		} catch (IOException e) {
			throw new CustomException("文件上传失败");
		}
		//解析上传成功的结果
		DefaultPutRet putRet = JSONUtil.toBean(response.bodyString(), DefaultPutRet.class);
		log.info("{七牛图片上传key: " + putRet.key + ",七牛图片上传hash: " + putRet.hash + "}");
		return putRet.key;
	}

	/**
	 * 七牛云删除图片
	 * @param key 文件名
	 * @param qiNiuConfig
	 * @return
	 * @throws QiniuException
	 */
	public void deleteQiniu(String key, SystemConfig qiNiuConfig) throws QiniuException {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = setQiNiuArea(qiNiuConfig.getQiNiuArea());
		//生成上传凭证，然后准备上传
		String accessKey = qiNiuConfig.getQiNiuAccessKey();
		String secretKey = qiNiuConfig.getQiNiuSecretKey();
		String bucket = qiNiuConfig.getQiNiuBucket();
		//...其他参数参考类注释
		Auth auth = Auth.create(accessKey, secretKey);
		BucketManager bucketManager = new BucketManager(auth, cfg);

		Response response = null;
		try {
			response = bucketManager.delete(bucket, key);
		} catch (IOException e) {
			throw new CustomException("文件删除失败");
		}
		//解析删除成功的结果
		DefaultPutRet putRet = JSONUtil.toBean(response.bodyString(), DefaultPutRet.class);
		log.info("{七牛图片删除key: " + putRet.key + ",七牛图片删除hash: " + putRet.hash + "}");

	}

	/**
	 * 七牛云批量删除图片
	 * @param keyList 文件名列表
	 * @param qiNiuConfig
	 * @return
	 * @throws QiniuException
	 */
	public void batchDelete(String[] keyList, SystemConfig qiNiuConfig) throws QiniuException {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = setQiNiuArea(qiNiuConfig.getQiNiuArea());
		//生成上传凭证，然后准备上传
		String accessKey = qiNiuConfig.getQiNiuAccessKey();
		String secretKey = qiNiuConfig.getQiNiuSecretKey();
		String bucket = qiNiuConfig.getQiNiuBucket();
		//...其他参数参考类注释
		Auth auth = Auth.create(accessKey, secretKey);
		BucketManager bucketManager = new BucketManager(auth, cfg);

		BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();

		batchOperations.addDeleteOp(bucket, keyList);

		Response response = null;
		try {
			response = bucketManager.batch(batchOperations);
		} catch (IOException e) {
			throw new CustomException("批量删除文件失败");
		}
	}

	/**
	 * 设置七牛云上传区域（内部方法）
	 *
	 * @param area
	 * @return
	 */
	private Configuration setQiNiuArea(String area) {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = null;

		switch (EQiNiuArea.valueOf(area).getCode()) {
			case "z0": {
				cfg = new Configuration(Region.huadong());
			}
			break;
			case "z1": {
				cfg = new Configuration(Region.huabei());
			}
			break;
			case "z2": {
				cfg = new Configuration(Region.huanan());
			}
			break;
			case "na0": {
				cfg = new Configuration(Region.beimei());
			}
			break;
			case "as0": {
				cfg = new Configuration(Region.xinjiapo());
			}
			break;
			default: {
				return null;
			}
		}
		return cfg;
	}


}
